package com.xmkj.carwash.mine.activity;


import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.cons.Constans;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.entity.bean.UserDetailsBean;
import com.common.retrofit.methods.UserInfoMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.rxbus.RxBus;
import com.common.rxbus.postevent.RxKeyEvent;
import com.common.utils.EmptyUtils;
import com.common.utils.SPUtils;
import com.common.widget.imageview.image.ImageLoaderUtils;
import com.common.widget.itemview.BaseItemLayout;
import com.common.widget.itemview.config.ConfigAttrs;
import com.common.widget.itemview.config.Mode;
import com.xmkj.carwash.R;
import com.xmkj.carwash.utils.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.functions.Action1;

/**
 * Created by chen on 2018/3/26.
 */

public class MineActivity extends BaseMvpActivity {
    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tv_compile)
    TextView mTvCompile;
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_id)
    TextView mTvId;
    @BindView(R.id.iv_face)
    ImageView mIvFace;
    @BindView(R.id.layout_mine)
    BaseItemLayout mLayoutMine;
    private ConfigAttrs attrs;
    private UserDetailsBean infoBean;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mine;
    }

    @Override
    protected void onViewCreated() {

    }

    @Override
    protected void doLogicFunc() {
        setMineItemView();
        attachClickListener(mTvCompile);
        attachClickListener(mIvBack);
        attachClickListener(mIvFace);
        gotoGetUserInfo();
        rxManager.add(RxBus.getDefault().toObservable(RxKeyEvent.class).subscribe(new Action1<RxKeyEvent>() {
            @Override
            public void call(RxKeyEvent rxKeyEvent) {
                if (EmptyUtils.isNotEventEmpty(rxKeyEvent, RxKeyEvent.UPDATE_INFO) && (Boolean) rxKeyEvent.getValue()) {
                    gotoGetUserInfo();



                }
            }
        }));
        
    }

    private void gotoGetUserInfo() {
        showProgressingDialog();
        CommonSubscriber<UserDetailsBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                dismissProgressDialog();
                infoBean = (UserDetailsBean) o;
                if(EmptyUtils.isNotEmpty(infoBean)){

                    if(EmptyUtils.isEmpty(infoBean.nickname)){
                        mTvName.setText("未设置姓名");
                    }else {
                        mTvName.setText(infoBean.nickname);
                    }
                    mTvId.setText("ID: " +infoBean.detail_id);
                    ImageLoaderUtils.displayCircle(mIvFace, infoBean.face);
                    SPUtils.setShareString(Constans.DETAILS_ID,infoBean.detail_id);
                }

            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

        UserInfoMethods.getInstance().userInfo(subscriber,uid,hashid);
        rxManager.add(subscriber);
    }

    @Override
    protected void onViewClicked(View view) {
        if(view.getId()==mTvCompile.getId()||view.getId()==mIvFace.getId()){
            Intent intent = new Intent(context,InfoCompileActivity.class);
            intent.putExtra(Constans.INFOBEAN,infoBean);
            startActivity(intent);
        }else if(view.getId()==mIvBack.getId()){
            onBackPressed();
        }
    }

    private void setMineItemView() {
        if (EmptyUtils.isNotEmpty(mLayoutMine) && mLayoutMine.getChildCount() > 0) {
            mLayoutMine.removeAllViews();
            mLayoutMine.getViewList().clear();
        }
        List<String> valueList = new ArrayList<>();
        List<Integer> resIdList = new ArrayList<>();
        valueList.add("我的钱包");
        valueList.add("车辆信息");
        valueList.add("洗车记录");
        valueList.add("我的优惠劵");
        valueList.add("发票申请");
        valueList.add("系统设置");

        resIdList.add(R.mipmap.wallet);
        resIdList.add(R.mipmap.wallet);
        resIdList.add(R.mipmap.record_wash);
        resIdList.add(R.mipmap.card);
        resIdList.add(R.mipmap.fapiao);
        resIdList.add(R.mipmap.setting);
        attrs = new ConfigAttrs();
        attrs.setValueList(valueList)  // 文字 list
                .setResIdList(resIdList) // icon list
                .setIconWidth(25)  //设置icon 的大小
                .setIconHeight(25)
                .setItemMode(Mode.TEXT)
                .setItemMarginTop(1)
                .setArrowResId(R.mipmap.next)
                .setMarginRight(12);
        mLayoutMine.setConfigAttrs(attrs).create();
        mLayoutMine.setOnBaseItemClick(new BaseItemLayout.OnBaseItemClick() {
            @Override
            public void onItemClick(int position) {
          
                switch (position) {
                    case 0:
                        Intent intent = new Intent(context,MyWalletActivity.class);
                        intent.putExtra(Constans.INFOBEAN,infoBean);
                        startActivity(intent);
                        break;
                    case 1:
                     gotoActivity(CarInfoActivity.class);
//                        return;
//                        gotoActivity(ApplyForBillActivity.class);
                        break;
                    case 2:
                        gotoActivity(WashCarRecordActivity.class);
//                        Intent intent2 = new Intent(context, BaseUrlActivity.class);
//                        intent2.putExtra(BaseUrlActivity.MAIN_URL, mineBean.kefu);
//                        intent2.putExtra(BaseUrlActivity.TITILE, "服务中心");
//                        context.startActivity(intent2);
////
//                        Intent surlIntent = new Intent(context, BaseUrlActivity.class);
//                        surlIntent.putExtra(BaseUrlActivity.MAIN_URL,mineBean.kefu);
//                        startActivity(surlIntent);
                        break;
                    case 3:
                        gotoActivity(MyCardActivity.class);
//                        Intent intent3 = new Intent(context, BaseUrlActivity.class);
//                        intent3.putExtra(BaseUrlActivity.MAIN_URL, mineBean.cjwt);
//                        intent3.putExtra(BaseUrlActivity.TITILE, "常见问题");
//                        context.startActivity(intent3);
                        break;
                    case 4:
                        //意见反馈
                        gotoActivity(FaPiaoApplyActivity.class);
//                        gotoActivity(FeedBackActivity.class);
                        break;
                    case 5:
                        //信用额度
//                        gotoActivity(CriditLimitActivity.class);
                        gotoActivity(SettingActivity.class);
                        break;

                }
            }
        });
    }

    @Override
    protected void setStatusBar() {
        StatusBarUtil.setStatusBarColor(this, R.color.main_color);
    }


    @Override
    protected void setNavigation() {
        super.setNavigation();
        m_statusBar.setVisibility(View.GONE);
    }

 

    @Override
    public void bindKnife() {
        ButterKnife.bind(this);

    }
}
